<template>
  <div class="app-container">
    <!--搜索-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公寓编号" prop="apartmentId">
        <el-input
          v-model="queryParams.apartmentName"
          placeholder="请输入公寓编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房客姓名" prop="name">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入房客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房客手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入房客手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同失效时间" prop="periodTime">
        <el-date-picker clearable
          v-model="queryParams.periodTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择合同失效时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增房客</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除房客</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--表格-->
    <el-table stripe v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" type="index" width="55" align="center" />
      <el-table-column label="房间号" width="100" align="center" prop="roomNum" />
      <el-table-column label="房客姓名" width="100" align="center" prop="tenantName" />
      <el-table-column label="房客手机号" align="center" prop="phone" />
      <el-table-column label="所属房源" align="center" prop="apartmentName" />
      <el-table-column label="房客身份证号" align="center" prop="idNumber" />
      <el-table-column label="入住时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同失效时间" align="center" prop="periodTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.periodTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加房客信息对话框 -->
    <el-dialog class="add-tenant-dialog" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form class="add-tenant-form" ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="选择房源" prop="apartmentName">
          <el-select v-model="form.apartmentId" placeholder="请选择房源" >
            <el-option
              v-for="(item, index) in apartmentList"
              :key="index"
              :label="item.apartmentName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间号" prop="roomId">
          <el-input v-model="form.roomNum" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="姓名" prop="tenantName">
          <el-input v-model="form.name" placeholder="请输入房客姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入房客手机号" maxlength="11"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入房客身份证号" maxlength="18"/>
        </el-form-item>
        <el-form-item label="合约到期" prop="periodTime">
          <el-date-picker clearable
            v-model="form.periodTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择合同失效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缴费时间" prop="collectionTime">
          <el-date-picker clearable
            v-model="form.collectionTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择缴费时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="房间类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入房间类型" />
        </el-form-item>
        <el-form-item label="租金" prop="rent">
          <el-input v-model="form.rent" placeholder="请输入房间租金" />
        </el-form-item>
        <el-form-item label="内置家具" prop="furniture">
          <el-input v-model="form.furniture" placeholder="请输入房间内置家具" />
        </el-form-item>
<!--        <el-form-item label="支付类型" prop="paymentType">-->
<!--          <el-select v-model="form.paymentType" placeholder="请选择房租支付类型" >-->
<!--            <el-option label="日付" value="Day"></el-option>-->
<!--            <el-option label="周付" value="Week"></el-option>-->
<!--            <el-option label="月付" value="Month"></el-option>-->
<!--            <el-option label="季付" value="Quarter"></el-option>-->
<!--            <el-option label="年付" value="Year"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addTenant">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listTenantApi, getTenantApi, delTenantApi, addTenantApi, updateTenantApi } from "@/api/apartment/tenant";
import { listHouseApi } from "@/api/apartment/house";
import item from "@/layout/components/Sidebar/Item.vue";

export default {
  name: "Info",
  computed: {
    item() {
      return item
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房客信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apartmentName: null,
        roomNum: null,
        tenantName: null,
        phone: null,
        idNumber: null,
        periodTime: null
      },
      apartmentList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        apartmentId: [
          { required: true, message: "请选择房源", trigger: "blur" }
        ],
        roomNum: [
          { required: true, message: "房间号不能为空", trigger: "blur" }
        ],
        tenantName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        periodTime: [
          { required: true, message: "合同失效时间不能为空", trigger: "blur" }
        ],
        collectionTime: [
          { required: true, message: "房租缴纳时间不能为空", trigger: "blur" }
        ],
        rent: [
          { required: true, message: "租金不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询房客信息列表 */
    getList() {
      this.loading = true;
      listTenantApi(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /**
     * 新增房客
     */
    addTenant() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addTenantApi(this.form).then(res => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      })
    },

    /** 查询所有房源 */
    getHouseList() {
      listHouseApi(this.queryParams).then(res =>{
        this.apartmentList = res.rows;
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        apartmentId: null,
        roomId: null,
        name: null,
        phone: null,
        idNumber: null,
        createTime: null,
        endTime: null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房客信息";
      this.getHouseList();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTenantApi(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房客信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTenantApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTenantApi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除该房客?').then(function() {
        return delTenantApi(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('/tenant/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>

.el-dialog__body{
  height: 70vh;
  overflow: auto;
  display: flex;
}



</style>
